package tijava.container;

import java.util.HashSet;
import java.util.Set;

import tijava.container.type.EqualsType;
import tijava.container.type.HashCodeType;
import tijava.container.type.HashType;
import tijava.container.type.BasicType;

/**
 * 使用HashSet的类必须同时重写equals和hashCode方法
 * 
 * @author nathan
 * @see HashType
 */
public class HashSetDemo {

	public static void main(String[] args) {
		typeDemo();
		basicSetTypeDemo();
		hashCodeTypeDemo();
		hashTypeDemo();
	}

	/**
	 * 使用Type对HashSet进行测试，对不同对象，equals为false，hashCode不相等<br>
	 * 任一Type的对象在HashSet中都是唯一的
	 * 
	 * @see BasicType
	 */
	private static void typeDemo() {
		System.out.println(".....typeDemo.....");
		Set<BasicType> set = new HashSet<BasicType>();
		set.add(new BasicType());
		set.add(new BasicType());
		set.add(new BasicType());
		set.add(new BasicType());

		BasicType t = new BasicType();
		set.add(t);
		set.add(t);

		System.out.println("the set's size is: " + set.size());
		for (BasicType i : set) {
			System.out.println(i + " 's hash code =" + i.hashCode());
		}
	}

	/**
	 * 使用EqualsType对HashSet进行测试，对不同对象，equals可能为true，hashCode不相等<br>
	 * 任一EqualsType的对象在HashSet中都是唯一的
	 * 
	 * @see EqualsType
	 */
	private static void basicSetTypeDemo() {
		System.out.println(".....basicSetTypeDemo.....");
		Set<BasicType> set = new HashSet<BasicType>();
		set.add(new EqualsType());
		set.add(new EqualsType());
		set.add(new EqualsType());
		set.add(new EqualsType());

		BasicType t = new EqualsType();
		set.add(t);
		set.add(t);

		System.out.println("the set's size is: " + set.size());
		for (BasicType i : set) {
			System.out.println(i + " 's hash code =" + i.hashCode());
		}
	}

	/**
	 * 使用HashCodeType对HashSet进行测试，对不同对象，equals可能为false，hashCode可能相等<br>
	 * 任一Type的对象在HashSet中都是唯一的
	 * 
	 * @see HashCodeType
	 */
	private static void hashCodeTypeDemo() {
		System.out.println(".....hashCodeTypeDemo.....");
		Set<BasicType> set = new HashSet<BasicType>();
		set.add(new HashCodeType('C', 4, "Empty trash"));
		set.add(new HashCodeType('C', 4, "Empty trash"));
		set.add(new HashCodeType('C', 4, "Empty trash"));
		set.add(new HashCodeType('C', 4, "Empty trash"));

		BasicType t = new HashCodeType('C', 4, "Empty trash");
		set.add(t);
		set.add(t);

		System.out.println("the set's size is: " + set.size());
		for (BasicType i : set) {
			System.out.println(i + " 's hash code =" + i.hashCode());
		}
	}

	/**
	 * 使用HashType对HashSet进行测试，对不同对象，equals可能为true，hashCode可能相等<br>
	 * 此时可以用HashSet去除重复对象（hashCode相等且是equals的）
	 * 
	 * @see HashType
	 */
	private static void hashTypeDemo() {
		System.out.println(".....hashTypeDemo.....");
		Set<BasicType> set = new HashSet<BasicType>();
		set.add(new HashType('C', 4, "Empty trash"));
		set.add(new HashType('C', 4, "Empty trash"));
		set.add(new HashType('A', 4, "Empty trash"));
		set.add(new HashType('C', 4, "Empty trash"));

		BasicType t = new HashType('A', 4, "Empty trash");
		set.add(t);
		set.add(t);

		System.out.println("the set's size is: " + set.size());
		for (BasicType i : set) {
			System.out.println(i + " 's hash code =" + i.hashCode());
		}
	}

}
